require(['config'], function() {
    require(['jquery', 'loadHeader', 'loadFooter', 'jquery.cookie'],($)=>{
        // $(function(){
        //     if($.cookie("userName")){
        //         $("#username").val($.cookie("userName"));
        //     }
        //     if($.cookie("eMail")){
        //         $("#email").val($.cookie("eMail"));
        //     }
        //     if($.cookie("iPhone")){
        //         $("#iphone").val($.cookie("iPhone"));
        //     }
        //     if($.cookie("passWord")){
        //         $("#pwd").val($.cookie("passWord"));
        //     }
        //     $("#submit").click(function(){
        //         console.log("123456");
        //         $.cookie("userName",$("#username").val(),{
        //             path: "/", expires: 7
        //         });
        //         $.cookie("eMail",$("#email").val(),{
        //             path: "/", expires: 7
        //         });
        //         $.cookie("iPhone",$("#iphone").val(),{
        //             path: "/", expires: 7
        //         });
        //         $.cookie("passWord",$("#password").val(),{
        //             path: "/", expires: 7
        //         });
        //     });
        // })

        //表单验证
        $('form').on('blur','#username, #pwd',function(e){
            //正则
            const usernameReg = /^[a-zA-Z]\w{3,19}$/
            const passwordReg = /^.{6,32}$/
            if($(e.target).is('#username')){
                const userName = $.trim($('#username').val())
                if(userName ==='' || (userName !== '' && !usernameReg.test(userName))){
                    alert('用户名格式不符合规范')
                    return
                }
            }
            if($(e.target).is('#pwd' )){
                const passWord = $.trim($('#pwd').val())
                if( passWord === '' || (passWord !== '' && !passwordReg.test(passWord))){
                    alert('密码格式不符合规格')
                    return
                }
            }
        })

        // 存储账号密码
        $('form').on('click', '#submit', e => {
            e.preventDefault()
            const usernameReg = /^[a-zA-Z]\w{3,19}$/
            const passwordReg = /^.{6,32}$/
            const userName = $.trim($('#username').val())
            const passWord = $.trim($('#pwd').val())
            if(usernameReg.test(userName) && passwordReg.test(passWord)){
                const username = $('#username').val()
                const password = $('#pwd').val()
                const info = {
                    username,
                    password
                }
                $.cookie.json = true
                const readCookie = $.cookie('info') || []
                const index = readCookie.findIndex(prod => prod.username === info.username)
                if(index !== -1) {
                    alert('用户名已存在，请重新输入')
                    e.preventDefault()
                }else{
                    readCookie.push(info)
                    alert('注册成功，将为你跳转到登录页面')
                    location.href = "/html/login.html"
                }
                $.cookie('info', readCookie, {expires : 180, path: '/'})
            }else{
                alert('注册失败，用户名或密码格式错误')
                return false
            }
        })
    })
})